package biz

import (
	"context"
	"hello-kratos/kratos-server/api/v1"

	"github.com/go-kratos/kratos/v2/errors"
	"github.com/go-kratos/kratos/v2/log"
)

var (
	// ErrUserNotFound is user not found.
	ErrUserNotFound = errors.NotFound(v1.ErrorReason_USER_NOT_FOUND.String(), "user not found")
)

type UserRepo interface {
	Save(context.Context, *User) error
	Update(context.Context, *User) error
	FindByID(context.Context, string) (*User, error)
	FindByAccount(context.Context, string) (*User, error)
	FindForLogin(context.Context, *User) (*User, error)
}

type UserUseCase struct {
	repo UserRepo
	log  *log.Helper
}

func NewUserUseCase(repo UserRepo, logger log.Logger) *UserUseCase {
	return &UserUseCase{repo: repo, log: log.NewHelper(logger)}
}

func (uc *UserUseCase) CreateUser(ctx context.Context, u *User) error {
	return uc.repo.Save(ctx, u)
}

func (uc *UserUseCase) FindForLogin(ctx context.Context, u *User) (*User, error) {
	return uc.repo.FindForLogin(ctx, u)
}
